import { Camera, EventTouch, Game, ResolutionPolicy, dragonBones, find, game, macro, math, profiler, v3, view } from "cc";
import { AppConfig } from "../config/AppConfig";
import { ServerConfig } from "../config/ServerConfig";
import { Logger } from "../engine/Logger";
import { ScreenShotHelper } from "../engine/ScreenShotHelper";
import { UIManager } from "../engine/UIManager";
import { MainScene } from "../scene/MainScene";
import { NativeSdkHelper } from "../sdk/NativeSdkHelper";
import { CommonConfig } from "../config/CommonConfig";
import { ResourceManager } from "../engine/ResourceManager";
import { ProtoManager } from "../lib/net/protobuf/ProtoManager";

export class GameController {

    private static _instance: GameController;
    public static get instance(): GameController {
        if (!GameController._instance) {
            GameController._instance = new GameController();
        }
        return GameController._instance;
    }

    public orientation: number; // 屏幕方向 

    private mainScene: MainScene

    private dianjiAni: dragonBones.ArmatureDisplay;

    public init(mainScene: MainScene) {
        this.mainScene = mainScene;
        NativeSdkHelper.init();
        ProtoManager.instance.init();
        // if (AppConfig.EnvironmentType.Dev == AppConfig.instance.environment) {
        //     profiler.showStats();
        // } else {
        //     profiler.hideStats()
        // }
        AppConfig.instance.DesignResolution.W = view.getDesignResolutionSize().width
        AppConfig.instance.DesignResolution.H = view.getDesignResolutionSize().height
        game.on(Game.EVENT_HIDE, this.onHideToBackground, this);
        game.on(Game.EVENT_SHOW, this.aWakeFromBackGround, this);
        this.setLandscape()
        ServerConfig.init()
        this.dianjiAni = find("uiRoot/dianjiAni", mainScene.node).getComponent(dragonBones.ArmatureDisplay)
        this.dianjiAni.node.active = false
        UIManager.instance.init(mainScene.node.getChildByName("uiRoot"))
        ScreenShotHelper.instance.init(mainScene.node.getChildByName("captureCamera").getComponent(Camera))
    }

    //重新开始游戏
    public restartGame() {
        // GameSocket.getInstance().closeSocket(false)
        UIManager.instance.closeAllView();
        game.restart()
    }

    private onHideToBackground(): void {
        Logger.log("onHideToBackground")
    }
    private aWakeFromBackGround(): void {
        Logger.log("aWakeFromBackGround")
    }

    //设置横屏
    public setLandscape() {
        this.orientation = macro.ORIENTATION_LANDSCAPE
        view.setOrientation(macro.ORIENTATION_LANDSCAPE);
        view.setDesignResolutionSize(AppConfig.instance.DesignResolution.W, AppConfig.instance.DesignResolution.H, ResolutionPolicy.FIXED_HEIGHT);
    }
    //设置竖屏
    public setPortrait() {
        this.orientation = macro.ORIENTATION_PORTRAIT
        view.setOrientation(this.orientation);
        view.setDesignResolutionSize(AppConfig.instance.DesignResolution.H, AppConfig.instance.DesignResolution.W, ResolutionPolicy.FIXED_WIDTH);
    }

    //点击事件
    public onCLickEvent(event: EventTouch) {
        let globalP: math.Vec2 = event.getUILocation()
        // Logger.log("onCLickEvent===", event.getUILocation(), view.getVisibleSize().width, view.getVisibleSize().height)
        globalP.x = globalP.x - view.getVisibleSize().width / 2;
        globalP.y = globalP.y - view.getVisibleSize().height / 2;
        GameController.instance.playDianji(globalP)
    }

    //播放点击动画
    private playDianji(p: math.Vec2) {
        // Logger.log("playDianji===", p)
        this.dianjiAni.node.active = true;
        this.dianjiAni.node.setPosition(v3(p.x, p.y));
        this.dianjiAni.once(dragonBones.EventObject.LOOP_COMPLETE, () => {
            this.dianjiAni.node.active = false
        }, this)
        this.dianjiAni.playAnimation("newAnimation", 1);
    }


    //预加载公共界面
    public preloadCommonView() {
        ResourceManager.instance.preload(CommonConfig.preLoadCommonView, null, (completedCount: number, totalCount: number, item: any) => {

        }, (resource: any[]) => {
        })
    }




}